package sis.searchtest;/*
 * @auther 顶风少年
 * @mail dfsn19970313@foxmail.com
 * @date 2019-07-31 19:01
 * @notify
 * @version 1.0
 */

import junit.framework.TestCase;
import sis.search.Search;
import sis.util.LineWriter;
import sis.util.TestUtil;

import java.io.FileNotFoundException;
import java.io.IOException;

public class SearchTest extends TestCase {
    //要搜索的字符串
    // private static final String URL = "http://www.langrsoft.com";

    public static final String[] TEST_HTML = {
            "<html>",
            "<body>",
            "Book: Agile Java, by Jeff Langr<br />",
            "Synopsis: Mr Langr teaches you<br />",
            "Java via test-driven development.<br />",
            "</body></html>"
    };

    public static final String FILE = "testFileHtmlSearch.html";
    public static final String URL = "file:" + FILE;

    public void setUp() throws IOException {
        TestUtil.delete(FILE);
        LineWriter.write(FILE, TEST_HTML);
    }

    public void testDown() throws IOException {
        TestUtil.delete(FILE);
    }

    //测试URL和要搜索的内容
    public void testCreate() throws IOException {
        Search search = new Search(URL, "x");
        assertEquals(URL, search.getUrl());
        assertEquals("x", search.getText());
    }

    //断言可以搜索到
    public void testPositiveSearch() throws IOException {
        Search search = new Search(URL, "Jeff langr");
        search.execute();
        assertTrue(search.matches() >= 1);
        //断言没有抛出异常
        assertFalse(search.errored());
    }

    //断言搜索不到
    public void testNegativeSearch() throws IOException {
        final String unlikelyText = "mama cass elliott";
        Search search = new Search(URL, unlikelyText);
        search.execute();
        assertEquals(0, search.matches());
        assertFalse(search.errored());
    }

    //断言抛出异常
    public void testErroredSearch() throws IOException {
        final String badUrl = URL + "/z2468.html";
        Search search = new Search(badUrl, "whatever");
        search.execute();
        //有异常
        assertTrue(search.errored());
        assertEquals(FileNotFoundException.class, search.getError().getClass());
    }
}
